package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Efficacy;
import com.adnaan.backend.service.EfficacyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * author: adnaan
 * create: 2025-02-04 20:15
 * Description:
 */
@Tag(name = "功效管理")
@RestControllerMapping("/efficacy")
@ApiSupport(author = "adnaan", order = 6)
public class EfficacyController {

    @Resource
    private EfficacyService efficacyService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "功效分页查询")
    public Result<Page<Efficacy>> pageQuery(@RequestBody PageParams<Efficacy> pageParams) {
        // 1.查询参数
        Efficacy params = pageParams.getParams();
        LambdaQueryWrapper<Efficacy> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, Efficacy::getName, params.getName());
        // 3.分页查询
        Page<Efficacy> page = efficacyService.page(pageParams.getPage(), lqw);
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增功效数据")
    public Result<Boolean> insert(@RequestBody Efficacy efficacy) {
        if (efficacyService.save(efficacy)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新功效数据")
    public Result<Boolean> update(@RequestBody Efficacy efficacy) {
        if (efficacyService.updateById(efficacy)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除功效数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (efficacyService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }
}
